package com.gexiaobao.pigeon.ui.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.model.bean.CarListBean
import com.gexiaobao.pigeon.ui.adapter.MyCarListAdapter
import com.yanzhenjie.recyclerview.SwipeRecyclerView

/**
 * @Author      : hxw
 * @Date        : 2024/7/4 9:17
 * @Describe    :
 */
class RxDialogCarList(context: Context?) : RxDialog(context!!) {

    private lateinit var leftBtn: AppCompatTextView
    private lateinit var rightBtn: AppCompatTextView
    private lateinit var swipeRecyclerView: SwipeRecyclerView
    private val adapter: MyCarListAdapter by lazy { MyCarListAdapter(arrayListOf()) }
    private lateinit var list: ArrayList<CarListBean>

    private var onClickCarListener: OnClickCarListener? = null

    init {
        initView()
    }

    @SuppressLint("MissingInflatedId")
    private fun initView() {
        val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_car_list, null)
        swipeRecyclerView = dialogView.findViewById(R.id.swipeRecyclerView)
        leftBtn = dialogView.findViewById(R.id.tvCancel)
        rightBtn = dialogView.findViewById(R.id.tvSure)

        // 添加系统默认的分割线
        swipeRecyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        swipeRecyclerView.layoutManager = LinearLayoutManager(context)

        adapter.setOnItemCarPassedListener(object : MyCarListAdapter.OnItemClickCarListener {
            override fun onItemCarPassed(id: Int) {
                onClickCarListener?.onCarPassed(id)
            }
        })

        setContentView(dialogView)
    }

    fun setSureListener(listener: View.OnClickListener?) {
        rightBtn.setOnClickListener(listener)
    }

    fun setCancelListener(listener: View.OnClickListener?) {
        leftBtn.setOnClickListener(listener)
    }

    fun setList(dataList: ArrayList<CarListBean>) {
        adapter.setList(dataList)
        swipeRecyclerView.adapter = adapter
        list = dataList
    }

    // 定义接口
    interface OnClickCarListener {
        fun onCarPassed(id: Int)
    }

    // 设置接口的实现者
    fun setOnCarPassedListener(listener: OnClickCarListener) {
        this.onClickCarListener = listener
    }

}